from flask import Flask, render_template,request,escape
import requests
import pandas as pd
from io import BytesIO
from xingzheng import geocode,around
from log_sys import log_request


app = Flask(__name__)
zhang_key = "241cb34c3ba21b934c74a65177c504ec"

@app.route('/entry')
def entry_page() -> 'html':
    title = '欢迎使用高德API查询小功能'
    return render_template('entry.html',
                           the_title=title)


@app.route('/site', methods=['POST'])
# 这些数据来自哪里？
def do_search() -> 'html':
    # location = request.form['location']   # 客户端input form
    color01 = request.form['color01']
    color02 = request.form['color02']
    # location = request.form['location']
    用户输入的结构化地址 = request.form['geocode']
    经纬度坐标 = geocode(zhang_key,用户输入的结构化地址)
    周边搜素结果 = around(zhang_key,经纬度坐标)
    position=[]
    for i,item in enumerate(用户输入的结构化地址):
        if item == '市':
            position.append(i)
    city=[]
    for item in position:
        city.append(用户输入的结构化地址[item-2:item+1])
    weather_i=[]
    # for i in weather:
    #     for key, value in i.items():
    #       print(key,":", value)

    title = '这里是你的结果：'

    print(request.form,request.remote_addr,request.user_agent)
    results = str(经纬度坐标)
    log_request(request,results)
    return render_template('results.html',
                           the_title=title,
                           the_color01=color01,
                           the_color02=color02,
                           the_geocode=用户输入的结构化地址,
                           the_location=经纬度坐标,
                           the_around=周边搜素结果,
                           # the_staticmap=地址地图,
                           the_results=results,)


@app.route('/viewlog')
def view_the_log() -> 'html':
    contents=[]
    with open('site.log','r') as log:
        for line in log:
            contents.append([])
            for item in line.split('|'):
                contents[-1].append(escape(item))
    len(contents)
    titles = ('用户提交','远程地址','访问代理','用户结果')
    return render_template('view.html',
                           the_title='日志系统后台',
                           the_row_titles=titles,
                           the_data=contents)



if __name__ == '__main__':
    app.run(debug=True)